package com.bl.student.service.biz.vip.impl;

import com.bl.student.common.enums.GoodsType;
import com.bl.student.common.enums.OnlineStatus;
import com.bl.student.common.enums.ValidStatus;
import com.bl.student.common.utils.ListUtils;
import com.bl.student.common.utils.UUIDUtils;
import com.bl.student.dao.model.GoodsEntity;
import com.bl.student.dao.model.UserEntity;
import com.bl.student.dao.model.VipEntity;
import com.bl.student.dao.redis.model.UserInfo;
import com.bl.student.dao.repository.GoodsRepository;
import com.bl.student.dao.repository.PayRepository;
import com.bl.student.dao.repository.UserRepository;
import com.bl.student.dao.repository.VipRepository;
import com.bl.student.service.biz.BaseService;
import com.bl.student.service.biz.vip.VipService;
import com.bl.student.service.vo.ListVo;
import com.bl.student.service.vo.vip.IsVipVo;
import com.bl.student.service.vo.vip.VipListVo;
import com.bl.student.service.vo.vip.VipVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yujingyi on 2018/2/25.
 */
@Service
@Log4j2
public class VipServiceImpl extends BaseService implements VipService {

    @Autowired
    VipRepository vipRepository;

    @Autowired
    GoodsRepository goodsRepository;

    @Autowired
    UserRepository userRepository;

    @Autowired
    PayRepository payRepository;

    @Override
    public VipEntity getVipByUserId(String userId) {
        List<VipEntity> vipEntities = vipRepository.findByUserId(userId);
        VipEntity vipEntity = ListUtils.getFirst(vipEntities);
        if (vipEntity != null) {
            handleVipStatus(vipEntity);
        }
        return vipEntity;
    }

    @Override
    public IsVipVo isVip(UserInfo userInfo) {
        IsVipVo isVipVo = new IsVipVo();
        isVipVo.setIsVip(false);
        VipEntity vipEntity = getVipByUserId(userInfo.getId());
        if (vipEntity != null) {
            handleVipStatus(vipEntity);
            if (vipEntity.getStatus() == ValidStatus.VALID) {
                isVipVo.setIsVip(true);
            }
            isVipVo.setExpireTime(vipEntity.getExpirationTime());
        }
        return isVipVo;
    }

    @Transactional
    public void handleVipStatus(VipEntity vipEntity) {
        if (vipEntity.getExpirationTime() < System.currentTimeMillis()
                && vipEntity.getStatus() == ValidStatus.VALID) {
            vipEntity.setStatus(ValidStatus.INVALID);
            vipRepository.save(vipEntity);
        }
    }

//    @Override
//    public ListVo<GoodsEntity> getVipItems(UserInfo userInfo) {
//        List<GoodsEntity> goodsEntities = goodsRepository.findByStatusAndTypeOrderByMonthsAsc(OnlineStatus.ONLINE, GoodsType.VIP)
//        return handleList(goodsEntities);
//    }

    @Override
    public VipListVo getVipList(Pageable pageable) {
        VipListVo vipListVo = new VipListVo();
        List<VipVo> vipVos = new ArrayList<>();

        Page<VipEntity> vipEntities = vipRepository.findAllByOrderByCreateTimeDesc(pageable);
        vipListVo.setTotalCount(vipEntities.getTotalElements());
        for (VipEntity vipEntity : vipEntities) {
            VipVo vipVo = new VipVo();
            UserEntity userEntity = userRepository.findOne(vipEntity.getUserId());
            vipVo.setUserEntity(userEntity);
            vipVo.setVipEntity(vipEntity);
            vipVos.add(vipVo);
        }

        vipListVo.setList(vipVos);

        return vipListVo;
    }

    @Override
    @Transactional
    public void createOrRenewUserVip(String userId, int months) {
        VipEntity vipEntity = getVipByUserId(userId);
        long now = System.currentTimeMillis();
        long duration = (long)months * 30 * 24 * 3600 * 1000;
        if (vipEntity == null) {
            vipEntity = new VipEntity();
            vipEntity.setId(UUIDUtils.getUUID());
            vipEntity.setStatus(ValidStatus.VALID);
            vipEntity.setCreateTime(now);
            vipEntity.setUserId(userId);
            vipEntity.setExpirationTime(now + duration);
            vipRepository.save(vipEntity);
        } else if (vipEntity.getStatus() == ValidStatus.INVALID) {
            vipEntity.setExpirationTime(now + duration);
            vipEntity.setUpdateTime(now);
            vipEntity.setStatus(ValidStatus.VALID);
        } else {
            vipEntity.setExpirationTime(vipEntity.getExpirationTime() + duration);
            vipEntity.setUpdateTime(now);
        }
    }

}
